<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class news extends CI_Controller {
		public function __construct()
		{
			parent::__construct();
			//$this->output->enable_profiler(TRUE);
			$this->load->helper('html');
			$this->load->helper('util');
			$this->load->library('form_validation');
			$this->load->config('config_ip');
			$this->load->library('pagination');
			$this->load->library('ckeditor');
			$this->load->library('ckfinder');
			$this->load->model('admin/login_model', 'login_model');		
			$this->load->model('admin/news_model', 'news_model');			
			session_start(); 
		}
		public function index( $offset = 0)
		{
			redirect('cms/news/lst/');
		}
		public function lst( $offset = 0)
		{
			$sort_by = 'time_update'; $sort_order = 'desc';
			if(isset($_SESSION['userinfo']))
			{       
				$data = array();
				$data['fields'] = array(
					'news_id' => 'ID',				
					'title' => 'Tiêu đề',
					'body' => 'Nội dung',
					'image' => 'Ảnh',		
					'type' => 'Kiểu',	
					'status' => 'Trạng thái',			
					'note' => 'Ghi chú',
					);
				// Access data
				$limit = 10;
				$result = $this->news_model->get_list_news($limit, $offset, $sort_by, $sort_order);
				$data['rows'] = $result['rows'];
				$data['num_rows'] = $result['num_rows'];
				// Paging
				$segment_array=$this->uri->segment_array();
				$segment_count=$this->uri->total_segments();
				$config = array();
				$config['base_url'] = site_url("cms/news/lst/");
				$config['total_rows'] = $data['num_rows'];
				$config['per_page'] = $limit;
				$config['uri_segment'] = 4;
				$this->pagination->initialize($config);
				$data['offset'] = $offset;
				$data['limit'] = $limit;
				$data['pagination'] = $this->pagination->create_links();
				// Sorting
				$data['sort_by'] = $sort_by;
				$data['sort_order'] = $sort_order;


				// Load view
				$this->load->view('admin/header_view',$data);
				$this->load->view('admin/news_view');
				$this->load->view('admin/footer_view',$data);
			
			}
			else
			{
				redirect('/cms/', 'refresh');
			}
		}
		
		public function add()
		{
			if(!isset($_SESSION['userinfo']))
			{       
				redirect('/cms/', 'refresh');				
			}
			$data = array();	
			if($this->input->post())
			{
				
				//echo  $_FILES['download_img']['name'];
				$newFileName = $_FILES['image']['name'];
				$fileExt = array_pop(explode(".", $newFileName));
				$fileOName = array_shift(explode(".", $newFileName));
				$fileOName = str_replace(" ", "_", $fileOName);
				$filename = $fileOName."_".time().".".$fileExt;

				$config2['upload_path'] = 'uploads/news/'.date("Y_m_d")."/";
				$config2['file_name'] = $filename;
				$config2['allowed_types'] = 'jpg|png|jpeg|gif';
				$config2['max_size']  = 1024 * 80;
				//$config['encrypt_name'] = TRUE;

				if (!file_exists($config2['upload_path'])) {
					mkdir($config2['upload_path'], 0777, true);
				}

				$this->load->library('upload', $config2);
				$this->upload->initialize($config2);
				if (!$this->upload->do_upload('image'))
				{
					echo $this->upload->display_errors();
					$data['error1'] = "Bạn chưa chọn ảnh";
					$img_file = "";
				} else {
					$file_data = $this->upload->data();
					$img_file = date("Y_m_d")."/".$file_data['file_name'];
				}

				$errorCode = $this->news_model->insert_news($this->input->post(),$img_file);
				if(isset($errorCode))
				{
					if($errorCode==200)
					{
						redirect("cms/news/");
					}
					else
					{
						$data['alert']=convert_errorcode($errorCode);
					}
				}		
		
				
			}
			
			$data['title_form']="Thêm tin tức";
			$data['news_id'] = 0;
			$data['type'] = "";
			$data['title'] = "";
			$data['body'] = "";
			$data['image'] = "";
			$data['time_update'] =null;
			$data['note'] = "";
			$data['status'] = 1;
			// Load view
			$this->load->view('admin/header_view',$data);
			$this->load->view('admin/news_update_view',$data);
			$this->load->view('admin/footer_view',$data);
		}
		public function edit($id){
			$data = array();
			$data['title_form']="Chỉnh sửa tin tức";
			if(!isset($_SESSION['userinfo']))
			{       
				redirect('/cms/', 'refresh');				
			}
			if($this->input->post())
			{
				if($_FILES['image']['name']!=''){
					//upload thumbnail      $c
					$newFileName = $_FILES['image']['name'];
					$fileExt = array_pop(explode(".", $newFileName));
					$fileOName = array_shift(explode(".", $newFileName));
					$fileOName = str_replace(" ", "_", $fileOName);
					$filename = $fileOName."_".time().".".$fileExt;

					$config['upload_path'] = 'uploads/news/'.date("Y_m_d")."/";
					$config['file_name'] = $filename;
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']  = 1024 * 80;
					//$config['encrypt_name'] = TRUE;

					if (!file_exists($config['upload_path'])) {
						mkdir($config['upload_path'], 0777, true);
					}


					$this->load->library('upload');
					$this->upload->initialize($config);
					if (!$this->upload->do_upload('image'))
					{
						$data['alert'] = $this->upload->display_errors();
						echo $this->upload->display_errors();
					}
					else
					{
						$upload_data = $this->upload->data();
						if($this->input->post('image')){
							unlink('uploads/news/'.$this->input->post('image'));
						}
						$img_file = date("Y_m_d")."/".$upload_data['file_name'];
						$errorCode=$this->news_model->update_news($this->input->post(),$img_file);
						if(isset($errorCode))
						{
							if($errorCode==200)
							{
								redirect("cms/news/");
							}
							else
							{
								$data['alert']=convert_errorcode($errorCode);
							}
						}		
					}
				} else {
					$errorCode=$this->news_model->update_news($this->input->post(),$this->input->post('image'));
					if(isset($errorCode))
					{
						if($errorCode==200)
						{
							redirect("cms/news/");
						}
						else
						{
							$data['alert']=convert_errorcode($errorCode);
						}
					}	
				}
				redirect("cms/news/");
			}

			$news_info = $this->news_model->get_news_by_id($id);
			$data['news_id'] = $news_info->news_id;
			$data['type'] =  $news_info->type;
			$data['title'] =  $news_info->title;
			$data['body'] =  $news_info->body;
			$data['image'] =  $news_info->image;
			$data['time_update'] = $news_info->time_update;
			$data['note'] = $news_info->note;
			$data['status'] =  $news_info->status;
			// Load view
			$this->load->view('admin/header_view',$data);
			$this->load->view('admin/news_update_view',$data);
			$this->load->view('admin/footer_view',$data);
		}
		public function delete($id){
			$news_data = $this->news_model->get_news_by_id($id);
			unlink('uploads/news/'.$news_data->image);
			$this->news_model->delete_news($id);
			redirect('cms/news/');
		}
	}
